package com.jcnet.bizaia;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.bean.SPSetting;
import com.jcnet.bizaia.bean.SPUserBean;
import com.jcnet.bizaia.bean.StateBean;
import com.jcnet.bizaia.bean.UserBean;
import com.jcnet.bizaia.bean.UserHttpBean;
import com.jcnet.bizaia.tools.BBTools;
import com.jcnet.bizaia.tools.DataStorageHelper;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/*private EditText userName;

	private EditText passWord;

	private EditText repeatPassWord;

	private Button commitButton;
	
	private String usernameString;
	private String passwordString;
	private String repeatPasswordString;
	
	
	userName = (EditText)findViewById(R.id.registerUserName);
	passWord = (EditText)findViewById(R.id.registerPassWord);
	repeatPassWord = (EditText)findViewById(R.id.registerRepeatPassWord);
	commitButton =(Button)findViewById(R.id.registerButton);*/
	public class RegisterActivity extends Activity implements OnClickListener{

	

	private Button registerButton;

	private ProgressHUD progressHUD;


	private String emailString;    //email

	private String pwdString;     //密码

	private String repeatPwdString;  //重复密码


	private EditText emailEditText,pwdEditText,repeatPwdEditText;


	private AsyncHttpClient asyncHttpClient;

	private Gson gson;

	private StringBuffer errorString;

	private RequestParams params;


	private DataStorageHelper storageHelper;
	
	private int jumpMark;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);

		
		jumpMark = getIntent().getIntExtra("jumpMark", -1);
		
		asyncHttpClient = new AsyncHttpClient();

		gson = new Gson();

		params =new RequestParams();


		registerButton  = (Button)findViewById(R.id.registerButton);

		emailEditText=(EditText)findViewById(R.id.registerUserName);
		pwdEditText=(EditText)findViewById(R.id.registerPassWord);
		repeatPwdEditText=(EditText)findViewById(R.id.registerRepeatPassWord);

		
		storageHelper =new DataStorageHelper(this, SPUserBean.dataTableNameForUser);

		
		registerButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub


				emailString = emailEditText.getText().toString().trim();

				pwdString = pwdEditText.getText().toString().trim();

				repeatPwdString = repeatPwdEditText.getText().toString().trim();

				errorString = new StringBuffer();

				 if(!BBTools.isEmptyString(emailString)){

					Toast.makeText(RegisterActivity.this,getString(R.string.emailNotNull), 0).show();
					//////System.out.println("2222");
					return;
				} 
				 
				 if(BBTools.isEmptyString(emailString)){
					
					if(!Toolkit.isEmail(emailString)){
						Toast.makeText(RegisterActivity.this,getString(R.string.emailFormatError), 0).show();
						emailEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.erroe,0 );
						return;
					}
						emailEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ok,0 );
					//////System.out.println("3333");
					
				}
				 
				 
				 if(!BBTools.isEmptyString(pwdString)){
					Toast.makeText(RegisterActivity.this,getString(R.string.pwdNotNull), 0).show();
					//////System.out.println("5555");
					return;
					
				} 
				
				if(BBTools.isEmptyString(pwdString)){
					if(pwdString.length()<=6){
					Toast.makeText(RegisterActivity.this,getString(R.string.pwdNotLength), 0).show();
					pwdEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.erroe,0 );
					return;
					}else{
						pwdEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ok,0 );
						
					}
				}
				
			
				
				
				if(!BBTools.isEmptyString(repeatPwdString)){
					Toast.makeText(RegisterActivity.this,getString(R.string.pwdReNotNull), 0).show();
					//////System.out.println("66666");
					return;
					
				} if(BBTools.isEmptyString(repeatPwdString)&&BBTools.isEmptyString(pwdString)){
					
					if(pwdString.equals(repeatPwdString)==false){
						//////System.out.println("77777");
						Toast.makeText(RegisterActivity.this,getString(R.string.pwdNotSame), 0).show();
						repeatPwdEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.erroe,0 );
						return;
					}else{
						repeatPwdEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ok,0 );
					}
				}

					//////System.out.println("_________________________");
					params.put("user.userpwd", BBTools.getMD5Str(pwdString));
					params.put("user.usermail", emailString);
					System.out.println("emailString="+emailString);
					params.put("languageNum", App.localeCode);

					asyncHttpClient.get(App.HTTP_PATH+"use_addUserOne.action?", params,new AsyncHttpResponseHandler(){
						@Override
						public void onStart() {
							// TODO Auto-generated method stub
							super.onStart();
							
							progressHUD = ProgressHUD.show(RegisterActivity.this, getString(R.string.registing), true, true, null);
							
						}

						public void onSuccess(String arg0) {

							
							
							System.out.println(arg0);
							if(arg0!=null){
								UserHttpBean stateBean = gson.fromJson(arg0, new TypeToken<UserHttpBean>(){}.getType());
								if(stateBean!=null&&stateBean.getState()==200){
									
									
									Toast.makeText(RegisterActivity.this, getString(R.string.registerOK), 0).show();
									
									UserBean userBean = stateBean.getObjJson();
									
									
									//存储保留数据到本地
									storageHelper.putInt(SPUserBean.userIDSP_1, userBean.getUserid());
									storageHelper.put(SPUserBean.regmailSP_6, userBean.getUsermail());
									storageHelper.put(SPUserBean.userImgSP_4, userBean.getUserimage());
									storageHelper.put(SPUserBean.nikeNameSP_2, userBean.getNickname());
									storageHelper.putInt(SPUserBean.vipsumSP_5, userBean.getVipsum());
									
									DataStorageHelper settingDataStorageHelper = new DataStorageHelper(RegisterActivity.this, SPSetting.SETTING_TABLE_NAME);
									
									if(jumpMark==100){
										startActivity(new Intent(RegisterActivity.this, MainActivity.class));
									}
									finish();
									
									
									
									}else{
									Toast.makeText(RegisterActivity.this, stateBean.getMessage(), 1).show();
								}

							}

						};

						@Override
						public void onFinish() {
							// TODO Auto-generated method stub
							super.onFinish();
							progressHUD.dismiss();
						}

						@Override
						public void onFailure(int arg0, Header[] arg1,
								byte[] arg2, Throwable arg3) {
							// TODO Auto-generated method stub
							super.onFailure(arg0, arg1, arg2, arg3);
							
							progressHUD.dismiss();
							
							Toast.makeText(RegisterActivity.this, getString(R.string.serverError), 1).show();
						}
					});
				

			}
		});
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
}
